// Movie Tech Agent
//
// This function represents the logic and activities of the movie tech in the movie theater simulation.
// This function will be forked onto a thread during initialization.
//

#include "MovieTechAgent.h"
#include "globalsExtern.cc"
#include "utility.h"

void MovieTechAgentExecute(int arg) {
	int TicketTakerID = arg;

	// Ticket Taker Agent running code should go here.
	DEBUG('m', "%s: Thread now running.\n", currentThread->getName());
	
	
	
	movieTechLock->Acquire();
	//wait until signaled to start movie
	movieTechCondition->Wait(movieTechLock);

	while (true) {
	   //Start the movie
	   DEBUG('R', "The MovieTechnician has started the movie.\n");
	   
	   //movie duration
	   for(int i=0; i<300;i++){
			currentThread->Yield();
	   }
	   DEBUG('R', "The MovieTechnician has ended the movie.\n");
	   //reset the numbers for available seats in the theater
	   theaterSelectionLock->Acquire();
	   totalsLock->Acquire();
	   totalsWhiteboard->setAvailableTickets(25-theaterSelectionWhiteboard->getSeatsAvail());
	   totalsLock->Release();
	   
	   theaterSelectionWhiteboard->movieOver();
	   //tell all the customers in the theater the movie is over
	   theaterSeatedCustomerCondition->Broadcast(theaterSelectionLock);
	   DEBUG('R', "The MovieTechnician has told all customers to leave the theater room.\n");
	   //tell the Ticket Takers they can start letting new customers into the next showing
	   theaterTicketTakerCondition->Broadcast(theaterSelectionLock);
	   
	   //signal the manager that the movie is over
	   managerLock->Acquire();
	   managerWhiteboard->setMovieRunning(false);
	   managerCondition->Signal(managerLock);
	   managerLock->Release();
	   
	   theaterSelectionLock->Release();
	   
	   //movie tech goes back to sleep till next movie begins
	   movieTechCondition->Wait(movieTechLock);
	}
}